<template>
  <div class="{{pathName}}-page page-main">
    <search-form :model="searchParams" @search="search">
      <a-form-item label="名称" name="prop">
        <a-input v-model:value="searchParams.prop" placeholder="请输入" />
      </a-form-item>
    </search-form>
    <div class="table-toolbar">
      <add-button @click="add()" />
    </div>
    <g-table
      ref="tableRef"
      :columns="columns"
      :api="getPageList"
      :params="searchParams"
    />

    <storeDetail
      v-model:visible="visible"
      :data="activeRow"
      @change="search(false)"
    />
  </div>
</template>

<script setup lang="tsx">
import { storeDel, getPageList } from "./api";
import type { IfreightSet } from "./entity";
import storeDetail from "./detail.vue";
import { FEE_MAP } from "@/utils/constant";
const searchParams = reactive({
  prop: "",
});

let visible = $ref(false);

const { tableRef, columns, search } = useTable<IfreightSet>([
  { title: "标题", dataIndex: "name" },
  {
    title: "计费方式",
    dataIndex: "type",
    customRender: ({ record }) => FEE_MAP[record.type],
  },
  {
    title: "指定包邮",
    dataIndex: "appoint",
    width: 100,
    customRender: ({ record }) => (record.appoint ? "开启" : "关闭"),
  },
  { title: "排序", dataIndex: "sort" },
  { title: "添加时间", dataIndex: "createTime" },
  {
    title: "操作",
    dataIndex: "op",
    customRender: ({ record }) => (
      <>
        <table-button onClick={() => add(record)}>编辑</table-button>
        <confirm-button danger onClick={() => remove(record)}>
          删除
        </confirm-button>
      </>
    ),
  },
]);

let activeRow = $ref<Partial<IfreightSet>>({});
function add(row: Partial<IfreightSet> = {}) {
  activeRow = { ...row };
  visible = true;
}

function remove({ id }: IfreightSet) {
  storeDel(id as number).then(() => {
    AntMessage.success("已删除");
    search(false);
  });
}
</script>

<style lang="scss"></style>
